package com.bdqn.service.impl;

import com.bdqn.entity.User;
import com.bdqn.mapper.UserMapper;
import com.bdqn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用户业务逻辑实现类
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Override
    @Transactional(readOnly = true)
    public List<User> getAllUsers() {
        return userMapper.selectAllUsers();
    }
    
    @Override
    @Transactional(readOnly = true)
    public User getUserById(Integer id) {
        if (id == null || id <= 0) {
            return null;
        }
        return userMapper.selectUserById(id);
    }
    
    @Override
    @Transactional(readOnly = true)
    public User getUserByUsername(String username) {
        if (username == null || username.trim().isEmpty()) {
            return null;
        }
        return userMapper.selectUserByUsername(username);
    }
    
    @Override
    public boolean addUser(User user) {
        if (user == null || user.getUsername() == null || user.getUsername().trim().isEmpty()) {
            return false;
        }
        
        // 检查用户名是否已存在
        User existUser = userMapper.selectUserByUsername(user.getUsername());
        if (existUser != null) {
            return false;
        }
        
        // 设置默认值
        if (user.getCreateTime() == null) {
            user.setCreateTime(new Date());
        }
        if (user.getStatus() == null) {
            user.setStatus(1);
        }
        
        return userMapper.insertUser(user) > 0;
    }
    
    @Override
    public boolean updateUser(User user) {
        if (user == null || user.getId() == null || user.getId() <= 0) {
            return false;
        }
        return userMapper.updateUser(user) > 0;
    }
    
    @Override
    public boolean deleteUser(Integer id) {
        if (id == null || id <= 0) {
            return false;
        }
        return userMapper.deleteUser(id) > 0;
    }
    
    @Override
    @Transactional(readOnly = true)
    public int getUserCount() {
        return userMapper.countUsers();
    }
    
    @Override
    public boolean initTestData() {
        try {
            // 创建测试用户数据
            User user1 = new User("admin", "123456", "admin@test.com", "13800138001");
            User user2 = new User("zhangsan", "123456", "zhangsan@test.com", "13800138002");
            User user3 = new User("lisi", "123456", "lisi@test.com", "13800138003");
            User user4 = new User("wangwu", "123456", "wangwu@test.com", "13800138004");
            
            // 插入测试数据
            userMapper.insertUser(user1);
            userMapper.insertUser(user2);
            userMapper.insertUser(user3);
            userMapper.insertUser(user4);
            
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}